%%%-------------------------------------------------------------------
%%% @author flowerpig
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(pool_player).

-behaviour(gen_server).
-include("include/logger.hrl").
-include("include/player.hrl").
-include("include/myshare.hrl").

-export([start_link/0, get_player/1, rand_player/1, put_player/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
  code_change/3]).

-define(SERVER, ?MODULE).

-record(pool_player_state, {player_number, login_number}).

%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================

start_link() ->
  gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
  Player_number = load_players(),
  {ok, #pool_player_state{player_number = Player_number, login_number = 0}}.

%%从账号池取账号
handle_call({rand_player, Num}, _From, State = #pool_player_state{player_number = Player_number, login_number = Login_number}) ->
  Players = get_player(Num),
  Number = erlang:length(Players),
  {reply, Players, State#pool_player_state{player_number = Player_number - Number, login_number = Login_number + Number}};
%%退出登陆后账号存入账号池
handle_call({put_player, Account, Password}, _From, State = #pool_player_state{login_number = Login_number, player_number = Player_number}) ->
  ets:insert_new(?ETS_POOL_PLAYER, [#player_data{account = Account, password = Password}]),
  {reply, ok, State#pool_player_state{player_number = Player_number + 1, login_number = Login_number - 1}};

handle_call(_Request, _From, State = #pool_player_state{}) ->
  {reply, ok, State}.

handle_cast(_Request, State = #pool_player_state{}) ->
  {noreply, State}.

handle_info(_Info, State = #pool_player_state{}) ->
  {noreply, State}.

terminate(_Reason, _State = #pool_player_state{}) ->
  ok.

code_change(_OldVsn, State = #pool_player_state{}, _Extra) ->
  {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
%%加载账号池玩家
load_players() ->
  Rows = database:players(),
  Players = [#player_data{account = binary_to_list(Account), password = binary_to_list(Password),
    is_new_player = Is_new_player} || [Account, Password, Is_new_player] <- Rows],
  ets:insert(?ETS_POOL_PLAYER, Players),
  erlang:length(Players).

%%取玩家数量
get_player(Number) ->
  case ets:tab2list(?ETS_POOL_PLAYER) of
    [] -> [];
    Players ->
      Total = erlang:length(Players),
      {Use_player, Rem_player} =
        case Total >= Number of
          true ->
            lists:split(Number, sys_util:do2(Players));
          _ ->
            {Players, []}
        end,
      ets:delete_all_objects(?ETS_POOL_PLAYER),
      ets:insert(?ETS_POOL_PLAYER, Rem_player),
      Use_player
  end.

%%获取指定数量账号
rand_player(Num) ->
  gen_server:call(pool_player, {rand_player, Num}).
%%存账号到账号池
put_player(Account, Password) ->
  ok = gen_server:call(pool_player, {put_player, Account, Password}).



